import os
from setup import *
from cpgzh import *
from pgzero.loaders import root
import json


class Classes:
    def __init__(self) -> None:
        "班级主界面"
        self.font = Font()
        self.pen = Pen()
        self.font.fontname = "simyou"
        bg1 = Actor(f"主场景背景")
        bg2 = Actor(f"主场景背景")
        bg1.scale = SCALE
        bg2.scale = SCALE
        bg1.left = 0
        bg2.left = bg1.right
        bg1.top = bg2.top = 0
        with open(os.path.join(root, "datas.json"), "r", encoding="utf-8") as f:
            classes = json.load(f)
            classes = classes["classes"]
        cnt=0
        for c in classes:
            a = Actor(
                os.path.join("物品购买交互图标", c["pic"]),
                midright=(300, 120 * SCALE + cnt % 4 * 100 * SCALE),
            )
            a.id = c["id"]
            a.name = c["name"]
            classes[cnt]=a
            cnt += 1
        classBG = Actor("队伍弹窗背景-动画_1-000", center=(480 * SCALE, 270 * SCALE))
        classBG.scale = SCALE
        self.classes = classes
        self.classBG = classBG
        self.page = 0
        self.lastPage = Actor("关卡切换按钮", center=(700 * SCALE, 420 * SCALE))
        for img in self.lastPage.images:
            if "关卡切换按钮/关卡切换按钮_上-normal-000.png" in img:
                self.lastPage.image = img
        self.lastPage.scale = 1
        self.nextPage = Actor("关卡切换按钮", center=(800 * SCALE, 420 * SCALE))
        for img in self.nextPage.images:
            if "关卡切换按钮/关卡切换按钮_下-normal-000.png" in img:
                self.nextPage.image = img
        self.nextPage.scale = 1
        self.bgs = [bg1, bg2]
        self.actors = [bg1, bg2, classBG, self.lastPage, self.nextPage] + classes

    def update(self):
        "更新数据"
        for bg in self.bgs:
            bg.x -= 1
            if bg.right <= 0:
                bg.left = 1440

    def draw(self):
        "画图了"
        for a in self.actors:
            a.draw()
        pen = self.pen
        font = self.font
        classes = self.classes[self.page * 4 : self.page * 4 + 4]
        for c in classes:
            pen.text(c.name, midleft=(c.x + c.width, c.y), font=font)


classes = Classes()
